#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import pathlib
import shutil

sys.path.append('../')

import yaml

from . import tp_logging
logger = tp_logging.get_logger('tp_config')


HOME = str(pathlib.Path.home())
TOPLY_DIR = os.path.join(HOME, 'toply')
DEFAULT_CONFIG_FILE = os.path.join(
    os.path.dirname(__file__),
    '../config.yaml')
CONFIG_FILE = os.path.join(TOPLY_DIR, 'config.yaml')

def manage_toply_dir():
    if not os.path.exists(TOPLY_DIR):
        logger.info('Creating toply directory at $HOME/toply')
        os.makedirs(TOPLY_DIR)

def manage_config_file():
    manage_toply_dir()
    if not os.path.isfile(CONFIG_FILE):
        logger.info(
            'Creating toply default config file at $HOME/toply/config.yaml')
        shutil.copyfile(DEFAULT_CONFIG_FILE, CONFIG_FILE)

def get_config():
    manage_config_file()
    with open(CONFIG_FILE, 'r+') as open_config_file:
        return yaml.load(open_config_file)

